package com.capgemini.fs.app.ekms.database;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.capgemini.fs.app.ekms.model.Forum;
import com.capgemini.fs.app.ekms.model.Functionality;
import com.capgemini.fs.app.ekms.model.Group;
import com.capgemini.fs.app.ekms.model.Reply;
import com.capgemini.fs.app.ekms.model.Role;
import com.capgemini.fs.app.ekms.model.Topic;
import com.capgemini.fs.app.ekms.model.User;
import com.capgemini.fs.app.ekms.persistence.util.DatabaseHelper;
import com.capgemini.fs.app.ekms.service.ForumService;
import com.capgemini.fs.app.ekms.service.FunctionalityService;
import com.capgemini.fs.app.ekms.service.GroupService;
import com.capgemini.fs.app.ekms.service.ReplyService;
import com.capgemini.fs.app.ekms.service.RoleService;
import com.capgemini.fs.app.ekms.service.TopicService;
import com.capgemini.fs.app.ekms.service.UserService;

public class ImportData {

	BeanFactory factory = new ClassPathXmlApplicationContext(
			"application-context.xml");
	FunctionalityService functionalityService = (FunctionalityService) factory
			.getBean("functionalityService");
	GroupService groupService = (GroupService) factory.getBean("groupService");
	RoleService roleService = (RoleService) factory.getBean("roleService");
	UserService userService = (UserService) factory.getBean("userService");
	ForumService forumService = (ForumService) factory.getBean("forumService");
	TopicService topicService = (TopicService) factory.getBean("topicService");
	ReplyService replyService = (ReplyService) factory.getBean("replyService");

	@Test
	public void importData() {
		extraSchema();
		createFunctionality();
		createRole();
		createGroup();
		createUser();
		createForum();
		addForumMembers();
		addForumModerators();
		addForumTopics();
		addReplies();
	}

	// Build Database
	public void extraSchema() {
		try {
			DatabaseHelper.createSchema();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// Create Functionalities
	public void createFunctionality() {
		Functionality f1 = new Functionality();
		Functionality f2 = new Functionality();
		Functionality f3 = new Functionality();
		Functionality f4 = new Functionality();
		Functionality f5 = new Functionality();
		Functionality f6 = new Functionality();
		Functionality f7 = new Functionality();
		Functionality f8 = new Functionality();
		Functionality f10 = new Functionality();
		Functionality f11 = new Functionality();
		Functionality f12 = new Functionality();
		f1.setFunctionalityName("Create Role");
		f1.setFunctionalityCode("F1");
		f1.setURL("/roleMgt/createRole");
		f1.setCategory("Role Mgmt");
		f1.setDisplayOrder(1);
		f2.setFunctionalityName("Maintain Role");
		f2.setFunctionalityCode("F2");
		f2.setURL("/roleMgt/maintainRole");
		f2.setCategory("Role Mgmt");
		f2.setDisplayOrder(2);
		f3.setFunctionalityName("Create User");
		f3.setFunctionalityCode("F3");
		f3.setURL("/user/createUser");
		f3.setCategory("User Mgmt");
		f3.setDisplayOrder(3);
		f4.setFunctionalityName("Maintain User");
		f4.setFunctionalityCode("F4");
		f4.setURL("/user/modifyUser");
		f4.setCategory("User Mgmt");
		f4.setDisplayOrder(4);
		f5.setFunctionalityName("Create Forum");
		f5.setFunctionalityCode("F5");
		f5.setURL("/forum/createForum");
		f5.setCategory("Forum Mgmt");
		f5.setDisplayOrder(5);
		f6.setFunctionalityName("Visit Forum");
		f6.setFunctionalityCode("F6");
		f6.setURL("/forum/visitForum");
		f6.setCategory("Forum Mgmt");
		f6.setDisplayOrder(6);

		f7.setFunctionalityName("Create Group");
		f7.setFunctionalityCode("F7");
		f7.setURL("/user/createGroup");
		f7.setCategory("User Mgmt");
		f7.setDisplayOrder(7);

		f8.setFunctionalityName("Modify Group");
		f8.setFunctionalityCode("F8");
		f8.setURL("/user/modifyGroup");
		f8.setCategory("User Mgmt");
		f8.setDisplayOrder(8);

		f10.setFunctionalityName("Modify Forum Category");
		f10.setFunctionalityCode("F10");
		f10.setURL("/forum/modifyCategory");
		f10.setCategory("Forum Mgmt");
		f10.setDisplayOrder(10);

		f11.setFunctionalityName("Modify Forum");
		f11.setFunctionalityCode("F11");
		f11.setURL("/forum/modifyForum");
		f11.setCategory("Forum Mgmt");
		f11.setDisplayOrder(11);

		f12.setFunctionalityName("Create Topic");
		f12.setFunctionalityCode("F12");
		f12.setURL("/forum/createTopic");
		f12.setCategory("Forum Mgmt");
		f12.setDisplayOrder(12);

		functionalityService.createFunctionality(f1);
		functionalityService.createFunctionality(f2);
		functionalityService.createFunctionality(f3);
		functionalityService.createFunctionality(f4);
		functionalityService.createFunctionality(f5);
		functionalityService.createFunctionality(f6);
		functionalityService.createFunctionality(f7);
		functionalityService.createFunctionality(f8);
		functionalityService.createFunctionality(f10);
		functionalityService.createFunctionality(f11);
		functionalityService.createFunctionality(f12);

	}

	// Create Role
	public void createRole() {

		Role role = new Role();
		role.setName("Admin");
		role.setStatus(0);
		Role role2 = new Role();
		role2.setName("Visitor");
		role2.setStatus(0);
		Functionality functionality3 = functionalityService
				.getFunctionalityByName("Visit Forum");
		Set<Functionality> functionalities2 = new HashSet<Functionality>();
		functionalities2.add(functionality3);
		role2.setFunctionalities(functionalities2);
		Set<Functionality> functionalities = new HashSet<Functionality>();
		List<Functionality> functionalities1 = functionalityService
				.getAllFunctionalities();
		for (Functionality functionality : functionalities1) {
			functionalities.add(functionality);
		}
		role.setFunctionalities(functionalities);
		RoleService roleService = (RoleService) factory.getBean("roleService");
		roleService.getPersistenceService().getRoleDAO().save(role);
		roleService.getPersistenceService().getRoleDAO().save(role2);
	}

	// Create Group
	public void createGroup() {
		Group group1 = new Group();
		group1.setGroupName("Group1");
		group1.setStatus(0);
		try {
			groupService.createGroup(group1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Create User
	public void createUser() {
		User user1 = new User();
		User user2 = new User();
		User user3 = new User();
		User user4 = new User();
		User user5 = new User();
		User user6 = new User();
		user1.setUserName("admin");
		user1.setSecurityPassword("admin");
		user1.setFirstName("Rick");
		user1.setLastName("Zhang");
		user1.setStatus(0);
		user1.setMemberType(0);

		user2.setUserName("Rick");
		user2.setSecurityPassword("111111");
		user2.setFirstName("Rick");
		user2.setLastName("Zhang");
		user2.setStatus(0);
		user2.setMemberType(0);

		user3.setUserName("Jone");
		user3.setSecurityPassword("111111");
		user3.setFirstName("Jone");
		user3.setLastName("Jin");
		user3.setStatus(0);
		user3.setMemberType(0);
		user4.setUserName("Sibylla");
		user4.setSecurityPassword("111111");
		user4.setFirstName("Sibylla");
		user4.setLastName("Wang");
		user4.setStatus(0);
		user4.setMemberType(0);
		user5.setUserName("Joe");
		user5.setSecurityPassword("111111");
		user5.setFirstName("Joe");
		user5.setLastName("Ye");
		user5.setStatus(0);
		user5.setMemberType(0);
		user6.setUserName("April");
		user6.setSecurityPassword("111111");
		user6.setFirstName("April");
		user6.setLastName("Zhou");
		user6.setStatus(0);
		user6.setMemberType(0);
		Role role = roleService.getRoleByName("Admin");
		Set<Role> roles = new HashSet<Role>();
		roles.add(role);
		user1.setRoles(roles);
		user2.setRoles(roles);
		user3.setRoles(roles);
		user4.setRoles(roles);
		user5.setRoles(roles);
		user6.setRoles(roles);
		userService.createUser(user1);
		userService.createUser(user2);
		userService.createUser(user3);
		userService.createUser(user4);
		userService.createUser(user5);
		userService.createUser(user6);
	}

	// Create Forum
	public void createForum() {
		Forum forum1 = new Forum();
		Forum forum2 = new Forum();
		Forum forum3 = new Forum();
		Forum forum4 = new Forum();
		Forum forum5 = new Forum();
		Forum forum6 = new Forum();
		Forum forum7 = new Forum();
		Forum forum8 = new Forum();
		Forum forum9 = new Forum();
		Forum forum10 = new Forum();
		forum1.setName("Post installation");
		forum2.setName("Before you start");
		forum3.setName("Installing Drupal");
		forum4.setName("Upgrading Drupal");
		forum5.setName("Converting to Drupal");
		forum6.setName("Module development");
		forum7.setName("Theme development");
		forum8.setName("Translations");
		forum9.setName("News and announcements");
		forum10.setName("General discussion");
		forum1.setStatus(0);
		forum2.setStatus(0);
		forum3.setStatus(0);
		forum4.setStatus(0);
		forum5.setStatus(0);
		forum6.setStatus(0);
		forum7.setStatus(0);
		forum8.setStatus(0);
		forum9.setStatus(0);
		forum10.setStatus(0);
		forum1.setDescription("Drupal is up and running but how do I ...?");
		forum2
				.setDescription("Is Drupal a viable solution for my website? Please see the documentation Before you start before posting.");
		forum3
				.setDescription("Installing Drupal? Please see the documentation in the handbook and the video resources for Drupal 5 and Drupal 6 for additional installation resources.");
		forum4
				.setDescription("Questions regarding upgrading an existing Drupal site. Don't forget to read the UPGRADE.txt that comes with evey Drupal download.");
		forum5.setDescription("Need help migrating your site to Drupal?");
		forum6.setDescription("For assistance with module development.");
		forum7.setDescription("For assistance with theme development.");
		forum8
				.setDescription("For translating the Drupal user interface.See also the Translations Group on groups.drupal.org");
		forum9
				.setDescription("For news and announcements to the Drupal community at large.");
		forum10
				.setDescription("For less technical discussions about the Drupal project. Not for support questions!");
		forumService.createForum(forum1);
		forumService.createForum(forum2);
		forumService.createForum(forum3);
		forumService.createForum(forum4);
		forumService.createForum(forum5);
		forumService.createForum(forum6);
		forumService.createForum(forum7);
		forumService.createForum(forum8);
		forumService.createForum(forum9);
		forumService.createForum(forum10);
	}

	public void addForumMembers() {
		userService.joinForum(1, 1);
		userService.joinForum(1, 2);
		userService.joinForum(1, 3);
		userService.joinForum(1, 4);

		userService.joinForum(1, 5);
		userService.joinForum(2, 2);
		userService.joinForum(2, 3);
		userService.joinForum(2, 4);

		userService.joinForum(3, 1);
		userService.joinForum(3, 2);
		userService.joinForum(3, 3);
		userService.joinForum(3, 4);

		userService.joinForum(4, 1);
		userService.joinForum(4, 2);
		userService.joinForum(4, 3);
		userService.joinForum(4, 4);
	}

	public void addForumModerators() {
		forumService.addModerator(1, 1);
		forumService.addModerator(1, 2);
		forumService.addModerator(1, 3);

		forumService.addModerator(2, 2);
		forumService.addModerator(2, 3);
		forumService.addModerator(2, 4);

		forumService.addModerator(3, 1);
		forumService.addModerator(3, 2);
		forumService.addModerator(3, 3);

		forumService.addModerator(1, 1);
		forumService.addModerator(1, 2);
		forumService.addModerator(1, 3);
	}

	public void addForumTopics() {

		Topic topic1 = new Topic();
		topic1.setSubject("Login Destination Module help");
		topic1
				.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic1.setStatus(0);
		topic1.setViewsCount(3);

		Topic topic2 = new Topic();
		topic2.setSubject("By date search - what is the best module to use?");
		topic2.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic2.setStatus(0);
		topic2.setViewsCount(4);
		
		Topic topic3 = new Topic();
		topic3.setSubject("How do I improve Drupal search");
		topic3.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic3.setStatus(0);
		topic3.setViewsCount(5);

		Topic topic4 = new Topic();
		topic4.setSubject("drupal book");
		topic4.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic4.setStatus(0);
		topic4.setViewsCount(345);

		Topic topic5 = new Topic();
		topic5.setSubject("One database and two websites.");
		topic5.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic5.setStatus(0);
		topic5.setViewsCount(55);

		Topic topic6 = new Topic();
		topic6.setSubject("Chat Room");
		topic6.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic6.setStatus(0);
		topic6.setViewsCount(77);

		Topic topic7 = new Topic();
		topic7.setSubject("Top of page link for long pages");
		topic7.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic7.setStatus(0);
		topic7.setViewsCount(11);

		Topic topic8 = new Topic();
		topic8.setSubject("Method to detect which CMS used...");
		topic8.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic8.setStatus(0);
		topic8.setViewsCount(99);

		Topic topic9 = new Topic();
		topic9.setSubject("Google Wave Invite");
		topic9.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic9.setStatus(0);
		topic9.setViewsCount(3);

		Topic topic11 = new Topic();
		topic11.setSubject(" 	Windows Live Writer and Other Blog Programs");
		topic11.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic11.setStatus(0);
		topic11.setViewsCount(34);

		Topic topic12 = new Topic();
		topic12.setSubject("Does this have to do with pinging?");
		topic12.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic12.setStatus(0);
		topic12.setViewsCount(34);

		Topic topic13 = new Topic();
		topic13.setSubject("resize old thumbnails, generated by views? ");
		topic13.setContent("Hi All,I can't quite figure out the correct PHP snippet that's required to get the Login Destination module to redirect"
						+ "users to the Workspace module. In other words, I would like Workspace to be the logged-in-user's home page (or dashboard)"
						+ "Can anyone help with the correct PHP snippet? or an alternative method if available?"
						+ "Drupal 6.13"
						+ "Apache 2.2.11"
						+ "PHP 5.2.9"
						+ "MySQL 5.1.30" +

						"Thanks in advance... Jack");
		topic13.setStatus(0);
		topic13.setViewsCount(36);
		
		topicService.createTopic(topic1, userService.getUserById(1),
				forumService.getForumById(1));
		topicService.createTopic(topic2, userService.getUserById(1),
				forumService.getForumById(1));
		topicService.createTopic(topic3, userService.getUserById(1),
				forumService.getForumById(1));
		topicService.createTopic(topic4, userService.getUserById(1),
				forumService.getForumById(2));
		topicService.createTopic(topic5, userService.getUserById(2),
				forumService.getForumById(3));
		topicService.createTopic(topic6, userService.getUserById(2),
				forumService.getForumById(3));
		topicService.createTopic(topic7, userService.getUserById(1),
				forumService.getForumById(1));
		topicService.createTopic(topic8, userService.getUserById(1),
				forumService.getForumById(1));
		topicService.createTopic(topic9, userService.getUserById(1),
				forumService.getForumById(2));
		topicService.createTopic(topic11, userService.getUserById(4),
				forumService.getForumById(4));
		topicService.createTopic(topic12, userService.getUserById(1),
				forumService.getForumById(4));
		topicService.createTopic(topic13, userService.getUserById(4),
				forumService.getForumById(3));

	}

	public void addReplies() {
		for (int i = 0; i < 20; i++) {
			Reply reply = new Reply();
			reply.setSubject("Subject " + i);
			reply.setContent("This is content " + i);
			reply.setStatus(0);
			replyService.createReply(reply, userService.getUserById(2),
					topicService.getTopicById(1));
		}
	}
}
